package grape.attend.service.classe.impl;

import grape.attend.service.classe.po.Classe;
import grape.attend.service.classe.mapper.ClasseMapper;
import grape.attend.service.classe.api.IClasseService;
import grape.attend.service.group.api.IGroupClasseRelService;
import grape.attend.service.group.po.GroupClasseRel;
import grape.common.service.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 班次设置表 服务实现类
 * </p>
 *
 * @author yangwei
 * @since 2020-05-08
 */
@Service
public class ClasseServiceImpl extends BaseServiceImpl<ClasseMapper, Classe> implements IClasseService {

    @Autowired
    @Lazy
    private IGroupClasseRelService iGroupClasseRelService;
    @Override
    public List<Classe> getByGroupId(String groupId) {
        assertParamNotEmpty(groupId,"groupId不能为空");
        List<GroupClasseRel> groupClasseRels = iGroupClasseRelService.getByGroupId(groupId);
        if (!isEmpty(groupClasseRels)) {
            Collection<Classe> classes = listByIds(groupClasseRels.stream().map(rel -> rel.getClasseId()).collect(Collectors.toList()));
            return (List<Classe>) classes;
        }
        return null;
    }

    @Override
    public Classe getByGroupIdAndClasseId(String groupId, String classeId) {
        assertParamNotEmpty(groupId,"groupId不能为空");
        assertParamNotEmpty(classeId,"classeId不能为空");
        List<GroupClasseRel> groupClasseRels = iGroupClasseRelService.getByGroupId(groupId);
        if (!isEmpty(groupClasseRels)) {
            Collection<Classe> classes = listByIds(groupClasseRels.stream().map(rel -> rel.getClasseId()).collect(Collectors.toList()));
            if (!isEmpty(classes)) {
                return classes.stream().filter(classe -> isEqual(classeId, classe.getId())).findFirst().orElse(null);
            }
        }
        return null;
    }
}
